package com.xbh.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.xbh.common.web.core.Result;
import com.xbh.file.service.FileTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: file-center
 * @description:
 * @author: 许宝华
 * @create: 2021-11-30 17:56
 */

@RestController
@RequestMapping("/file")
public class FileTransferController {
    @Autowired
    private FileTransferService fileTransferService;

    /**
     * 上传文本文件
     * @param str
     * @param fileName
     * @return
     */
    @PostMapping("/upload")
    public Result fileUpload(@RequestBody JSONObject params){
        String text = params.getString("text");
        String fileName = params.getString("fileName");
        String uploadUrl = fileTransferService.fileUpload(text,fileName);
        return Result.success(uploadUrl);
    }

    /**
     * 下载文本文件
     * @param params
     * @return
     */
    @PostMapping("/download")
    public Result fileDownload(@RequestBody JSONObject params){
        String downloadUrl = params.getString("downloadUrl");
        String resStr = fileTransferService.fileDownload(downloadUrl);
        return Result.success(resStr);
    }

    /** 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadPicture")
    public Result uploadPicture(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.failedParams("上传失败，请选择文件");
        }
        JSONObject jsonObject = fileTransferService.uploadPicture(file);
        if(jsonObject == null){

            return Result.failedParams("上传失败！");
        }
        return Result.success(jsonObject);
    }
}
